// pages/center/wallet/wallet.js
var app = getApp();
var util = require('../../../../utils/util.js');
const Request = require('../../../../utils/request.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabselect: 0,
    type: 2,
    page1: 0,
    page2: 0,

    //弹窗
    isShowDialog: false,
    // 提现规则
    withdrawData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '钱包',
    });

    // var that = this;
    // var query = wx.createSelectorQuery();
    // //选择id
    // query.select('#bar1').boundingClientRect();
    // query.select('#bar2').boundingClientRect()
    // query.exec(function(res) {
    //   console.log(res[0].height);
    //   console.log(res[1].height);
    //   console.log(app.globalData.systemInfo.screenHeight)
    //   that.setData({
    //     scrollViewHeigth: app.globalData.windowHeight - res[0].height - res[1].height - 10,
    //   })
    // })

    // this.setData({
    //   windowWidth: app.globalData.windowWidth,
    // });
    this.getUserInfo();
  },

  getUserInfo: function() {
    var that = this;
    var data = {
      point_url: app.server.adminApi.getUsersInfo,
      openid: app.globalData.userInfo.openid
    }
    app.server.getConnect(data).then(function(res) {
      wx.setStorageSync("userInfo", res.data);
      app.globalData.userInfo = res.data;
      that.setData({
        bonus: res.data.bonus,
        sale_bonus: res.data.sale_bonus,
        isShowDialog: false,
        withdrawData: res.data.withdrawData
      })
      that.loadData();
    })
  },

  bindSelectType: function() {
    let that = this;
    wx.showActionSheet({
      itemList: ['余额', '奖金'],
      success (res) {
        that.setData({
          type: res.tapIndex+1
        });
        that.loadData();
      }
    })
  },

  loadData: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    var that = this;
    Request.createAll([
      app.server.getConnect({
        point_url: app.server.adminApi.getBonusList,
        admin_id: app.globalData.userInfo.id,
        type: that.data.type
      }),
      app.server.getConnect({
        point_url: app.server.adminApi.getTakeMoneyList,
        admin_id: app.globalData.userInfo.id,
        type: that.data.type
      })
    ]).then(function(res) {
      that.setData({
        bonusList: res[0].data,
        takeMoneyList: res[1].data,
        page1: 0,
        page2: 1
      });
      wx.hideLoading();
    }, function(res) {
      // console.log(res);
      wx.hideLoading();
    })
  },

  loadBonusList: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })

    var that = this;
    this.data.page1 += 1;
    app.server.getConnect({
      point_url: app.server.adminApi.getBonusList,
      admin_id: app.globalData.userInfo.id,
      page: that.data.page1
    }).then(function(res) {
      var temp = that.data.bonusList;
      temp.push.apply(temp, res.data);
      that.setData({
        bonusList: temp
      });
      wx.hideLoading();
    }, function(res) {
      // console.log(res);
      wx.hideLoading();
    })
  },

  loadTakeMoneyList: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    var that = this;
    this.data.page2 += 1;
    app.server.getConnect({
      point_url: app.server.adminApi.getTakeMoneyList,
      admin_id: app.globalData.userInfo.id,
      page: that.data.page2
    }).then(function(res) {
      var temp = that.data.takeMoneyList;
      temp.push.apply(temp, res.data);
      that.setData({
        takeMoneyList: temp,
      });
      wx.hideLoading();
    }, function(res) {
      // console.log(res);
      wx.hideLoading();
    })
  },

  //滑动切换
  swiperTab: function(e) {
    var that = this;
    that.setData({
      tabselect: e.detail.current,
    });
    this.loadData();
  },

  //点击切换
  clickTab: function(e) {
    if (e.currentTarget.dataset.idx != this.data.tabselect) {
      this.setData({
        tabselect: e.currentTarget.dataset.idx
      });
      this.loadData();
    }
  },

  showDialog: function() {
    this.setData({
      isShowDialog: true
    })
  },

  hiddenDialog: function() {
    this.setData({
      isShowDialog: false
    })
  },

  bindsubmit: function(e) {
    this.hiddenDialog();
    var that = this;
    if (!e.detail.value.bonus) {
      app.showConfirm('提示', '请输入提现金额', false);
    } else if (e.detail.value.bonus < 1) {
      app.showConfirm('提示', '提现金额不小于1元,请重新输入', false);
    } else if (!util.checkString(e.detail.value.bonus)) {
      app.showConfirm('提示', '请输入大于1的正整数', false);
    } else {
      wx.showLoading({
        title: '提现中...',
        mask: true
      })
      console.log(e)
      app.server.getConnect({
        point_url: app.server.adminApi.takeMoney,
        admin_id: app.globalData.userInfo.id,
        bonus_money: e.detail.value.bonus,
        remarks: e.detail.value.remarks,
        form_id: e.detail.formId,
        type: this.data.type
      }).then(function(res) {
        // console.log(res);
        wx.hideLoading();
        wx.showToast({
          icon: 'success',
          title: '提现成功'
        });
        that.getUserInfo();
      }, function(res) {
        wx.hideLoading();
        // console.log(res);
        // app.showConfirm('提示', res.data.msg, false);
      })
    // } else {
    //   app.showConfirm('提示', '您的可提现金额不足,请重新输入', false);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.tabselect == 0) {
      this.loadBonusList();
    } else {
      this.loadTakeMoneyList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
